﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LibrarySync2.Settings;
using LibrarySync2.Business.FileSystem;

namespace LibrarySync2.Controls
{
    public partial class ucEditUserPreferences : BaseUserControl
    {
        public ucEditUserPreferences()
        {
            InitializeComponent();
            LoadPreferences();
        }

        private void btnSavePrefrences_Click(object sender, EventArgs e)
        {
            if (!ValidateInputs())
                return;

            SavePreferences();
        }

        private void LoadPreferences()
        {
            fssDefaultMediaDirectory.Path = UserPreferencesManager.Settings.DefaultMediaDirectory;
            fssDefaultLibraryFilePath.Path = UserPreferencesManager.Settings.DefaultLibraryFilePath;
            fssDefaultDestinationDirectory.Path = UserPreferencesManager.Settings.DefaultDestinationDirectory;
            fssDefaultBlacklistFilePath.Path = UserPreferencesManager.Settings.DefaultBlacklistFilePath;
        }

        private void SavePreferences()
        {
            UserPreferencesManager.Settings.DefaultMediaDirectory = fssDefaultMediaDirectory.Path;
            UserPreferencesManager.Settings.DefaultLibraryFilePath = fssDefaultLibraryFilePath.Path;
            UserPreferencesManager.Settings.DefaultDestinationDirectory = fssDefaultDestinationDirectory.Path;
            UserPreferencesManager.Settings.DefaultBlacklistFilePath = fssDefaultBlacklistFilePath.Path;
            UserPreferencesManager.SaveSettings(UserPreferencesManager.Settings);
            this.ShowMessage("Your preferences have been saved.");
        }

        private bool ValidateInputs()
        {
            if (string.IsNullOrEmpty(fssDefaultMediaDirectory.Path))
                return this.ShowErrorMessage("You must specify a local media directory.");

            if (string.IsNullOrEmpty(fssDefaultLibraryFilePath.Path))
                return this.ShowErrorMessage("You must specify a trading partner library file.");

            if (string.IsNullOrEmpty(fssDefaultDestinationDirectory.Path))
                return this.ShowErrorMessage("You must specify a destination directory.");

            return true;
        }
    }
}
